<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/3/30
 * Time: 9:54
 */

namespace Admin\Controller;

use Admin\Api\ConfigApi;
use Bbs\Api\FilterKeyWordsApi;
use Bbs\Api\ForumForumApi;
use Bbs\Api\ForumPostApi;

class BbsController extends AdminController{

    public function index(){

        $this->display();

    }

    /**
     * 编辑用户权限
     */
    public function MembersAccess(){

        if(IS_POST){
            $forumid = I('post.forumid','-2');
            $modnewposts = I('post.modnewposts','-2');

            if($forumid==-2 || $modnewposts==-2){
                $this->error('参数缺失');
            }

            if($modnewposts == -1){
                $this->success('操作成功');
            }

            if(!in_array($modnewposts,[0,1])){
                $this->error('参数错误');
            }

            $map = array('fid' => $forumid);
            $result = apiCall(ForumForumApi::SAVE,array($map,array('modnewposts' => $modnewposts)));
            if($result['status']){

                //如果开启审核,之前的帖子全部自动审核通过,之后的帖子才需要审核
                $map = array('fid' => $forumid);
                $result = apiCall(ForumPostApi::SAVE,array($map,array('invisible' => 1)));
                if($result['status']){
                    $this->success('操作成功');
                }else{
                    $this->error('操作失败');
                }

            }else{
                $this->error('操作失败');
            }

        }else{
            //查询论坛
            $order = 'displayorder asc';
            $result = apiCall(ForumForumApi::QUERY_NO_PAGING,array(null,$order));
            if($result['status']){
                $this->assign('forum_list',$result['info']);
            }else{
                $this->error('未知错误');
            }

            $this->display();
        }

    }

    public function FilterKeyWords(){

        if(IS_POST){
            $key_words = I('post.key_words','');
            $forumid = I('post.forumid','-2');

            $map = array('name'=>'FILTRATION_KEYWORDS');
            $entity = array('value'=>$key_words);

            $result = apiCall(ConfigApi::SAVE,array($map,$entity));
            if($result['status']){
                S("config_" . session_id() . '_' . session("uid"),null);
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }


        }else{
            //查询论坛
            $order = 'displayorder asc';
            $result = apiCall(ForumForumApi::QUERY_NO_PAGING,array(null,$order));
            if($result['status']){
                $this->assign('forum_list',$result['info']);
            }else{
                $this->error('未知错误');
            }

            $key_words = C('FILTRATION_KEYWORDS');
            $this->assign('key_words',$key_words);

            $this->display();
        }
    }


}